home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / test / word.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  8.1 KB  |  190 lines  |  [TEXT/R*ch]

  1. (* test/word.sml -- some test cases for Word, appropriate for a two's
  2.    complement machine whose Int.precision = SOME 31 
  3.    PS 1995-03-19, 1995-07-12, 1995-11-06, 1996-04-01, 1996-10-01 *)
  4.  
  5. use "auxil.sml";
  6.  
  7. local 
  8.     (* Isn't this disgusting: *)
  9.     val [gt,  lt,  ge,   le] = 
  10.     [op>, op<, op>=, op<=] : (int * int -> bool) list
  11.     val [add, sub, mul, idiv,   imod] = 
  12.     [op+, op-, op*, op div, op mod] : (int * int -> int) list
  13.     open Word;
  14.     val op > = gt and op < = lt and op >= = ge and op <= = le;
  15.     val op + = add and op - = sub and op * = mul 
  16.     and op div = idiv and op mod = imod;
  17.     val i2w = fromInt
  18.     and w2i = toInt;
  19. in
  20.  
  21. val test1 = checkrange (0, 1025)
  22.     (fn i => i = w2i (i2w i));
  23.  
  24. val test3 = checkrange (~1000, 1000) 
  25.     (fn i => i = toIntX (i2w i));
  26.  
  27. val test5a = checkrange (0,15) 
  28.     (fn i => (i+960) div 2 * 2 + 1
  29.              = w2i (orb (i2w i, i2w 961)));
  30. val test5b = checkrange (0,513)
  31.     (fn i => i = w2i (orb (i2w i, i2w i)));
  32. val test6a = checkrange (0,15) 
  33.     (fn i => i div 2 * 2 = w2i (andb (i2w i, i2w ~2)));
  34. val test6b = checkrange (0,513)
  35.     (fn i => i = w2i (andb (i2w i, i2w i)));
  36. val test7a = checkrange (0,15) 
  37.     (fn i => i+960 = w2i (xorb (i2w i, i2w 960)));
  38. val test7b = checkrange (0, 513)
  39.     (fn i => 0 = w2i (xorb (i2w i, i2w i)));
  40.  
  41. val test8a = check (~1 = w2i (notb (i2w 0)));
  42. val test8b = check (0 = w2i (notb (i2w ~1)));
  43.  
  44. val maxposint = valOf Int.maxInt;
  45. val maxnegint = ~maxposint-1;
  46. fun pwr2 0 = 1 
  47.   | pwr2 n = 2 * pwr2 (n-1);
  48. fun rwp i 0 = i
  49.   | rwp i n = rwp i (n-1) div 2;
  50.  
  51. val test9a = checkrange (0,29)
  52.     (fn k => pwr2 k = w2i (<< (i2w 1, i2w k)));
  53. val test9b = checkrange (31,65)
  54.     (fn k => 0 = w2i (<< (i2w 1, i2w k)));
  55. val test9c = check (maxnegint = w2i (<< (i2w 1, i2w 30)));
  56. val test9d = checkrange (0, 1025)
  57.     (fn i => 2 * i = w2i (<< (i2w i, i2w 1)));
  58. val test9e = checkrange (0, 1025)
  59.     (fn i => i div 2 = w2i (>> (i2w i, i2w 1)));
  60. val test9f = checkrange (0,65)
  61.     (fn k => rwp maxposint k = w2i (>> (i2w maxposint, i2w k)));
  62. val test9g = checkrange (31,65)
  63.     (fn k => 0 = w2i (<< (i2w ~1, i2w k)));
  64. val test9h = checkrange (1,65)
  65.     (fn k => 0 = w2i (>> (i2w 1, i2w k)));
  66.  
  67. val test10a = checkrange (1,65)
  68.     (fn k => 0 = w2i (~>> (i2w 1, i2w k)));
  69. val test10b = checkrange (1,65)
  70.     (fn k => ~1 = w2i (~>> (i2w ~1, i2w k)));
  71. val test10c = checkrange (~513, 513)
  72.     (fn i => i div 2 = toIntX (~>> (i2w i, i2w 1)));
  73. val test10d = checkrange (0,65)
  74.     (fn k => rwp maxnegint k = toIntX (~>> (i2w maxnegint, i2w k)));
  75.  
  76. local 
  77.     open Word
  78. in
  79. val test11a = check (i2w 256 > i2w 255);
  80. val test11b = check (i2w 0 < i2w ~1);
  81. val test11c = check (i2w maxposint >= i2w maxposint);
  82. val test11d = check (i2w maxnegint >= i2w 127);
  83. val test11e = check (i2w 1 <= i2w 1);
  84. val test11f = check (i2w 0 <= i2w 1);
  85. val test11g = check (i2w 0 < i2w maxposint);
  86. val test11h = check (i2w maxposint < i2w maxnegint);
  87. val test11i = check (i2w maxnegint < i2w ~1);
  88. end;
  89.  
  90. local 
  91.     open Word
  92. in
  93. val test12a = checkrange(0, 300) (fn k => w2i (i2w k + i2w 17) = add(k, 17));
  94. val test12b = checkrange(0, 300) (fn k => w2i (i2w k - i2w 17) = sub(k, 17));
  95. val test12c = checkrange(0, 300) (fn k => w2i (i2w k * i2w 17) = mul(k, 17));
  96. val test12d = checkrange(0, 300) 
  97.     (fn k => w2i (i2w k div i2w 17) = idiv(k, 17));
  98. val test12e = checkrange(0, 300) 
  99.     (fn k => w2i (i2w k mod i2w 17) = imod(k, 17));
  100. val test12f = checkrange(0, 300) 
  101.     (fn k => w2i (i2w k + i2w maxnegint) = add(k, maxnegint));
  102. val test12g = checkrange(0, 300) 
  103.     (fn k => w2i (i2w maxnegint - i2w k - i2w 1) = sub(maxposint,k));
  104. val test12h = checkrange(0, 300) 
  105.     (fn k => w2i (i2w k * i2w maxnegint) = mul(imod(k, 2), maxnegint));
  106. val test12i = checkrange(0, 300) 
  107.     (fn k => w2i (i2w k * i2w maxposint + i2w k) = mul(imod(k, 2), maxnegint));
  108. val test12j = checkrange(0, 300) 
  109.     (fn k => w2i (i2w k div i2w ~1) = 0);
  110. val test12k = checkrange(0, 300) 
  111.     (fn k => w2i (i2w k mod i2w ~1) = k);
  112. val test12l = check(w2i (i2w maxposint + i2w 1) = maxnegint);
  113. val test12m = check(w2i (i2w maxnegint - i2w 1) = maxposint);
  114. val test12n = check(w2i (i2w ~1 div i2w 2) = maxposint);
  115. val test12o = check(w2i (i2w ~1 mod i2w 2) = 1);
  116. val test12p = check(w2i (i2w ~1 div i2w 100) = idiv(maxposint, 50));
  117. val test12q = check(w2i (i2w ~1 mod i2w 10) = 7);
  118. val test12r = (i2w 17 div i2w 0 seq "WRONG") 
  119.               handle Div => "OK" | _ => "WRONG";
  120. val test12s = (i2w 17 mod i2w 0 seq "WRONG") 
  121.               handle Div => "OK" | _ => "WRONG";
  122.  
  123. fun chk f (s, r) = 
  124.     check'(fn _ => 
  125.        case f s of
  126.            SOME res => res = i2w r
  127.          | NONE     => false)
  128.  
  129. fun chkScan fmt = chk (StringCvt.scanString (scan fmt))
  130.  
  131. val test13a = 
  132.     List.map (chk fromString)
  133.              [("20Af", 8367),
  134.           (" \n\t20AfGrap", 8367),
  135.           ("0w20Af", 8367),
  136.           (" \n\t0w20AfGrap", 8367),
  137.           ("0", 0),
  138.           ("0w", 0),
  139.           ("0W1", 0),
  140.           ("0w ", 0),
  141.           ("0wx", 0),
  142.           ("0wX", 0),
  143.           ("0wx1", 1),
  144.           ("0wX1", 1),
  145.           ("0wx ", 0),
  146.           ("0wX ", 0)];
  147.  
  148. val test13b = 
  149.     List.map (fn s => case fromString s of NONE => "OK" | _ => "WRONG")
  150.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  151.         "+1", "~1", "-1", "GG"];        
  152.  
  153. val test14a = 
  154.     List.map (chkScan StringCvt.DEC)
  155.              [("10789", 10789),
  156.           (" \n\t10789crap", 10789),
  157.           ("0w10789", 10789),
  158.           (" \n\t0w10789crap", 10789),
  159.           ("0", 0),
  160.           ("0w", 0),
  161.           ("0W1", 0),
  162.           ("0w ", 0),
  163.           ("0wx", 0),
  164.           ("0wX", 0),
  165.           ("0wx1", 0),
  166.           ("0wX1", 0),
  167.           ("0wx ", 0),
  168.           ("0wX ", 0)];
  169.  
  170. val test14b = 
  171.     List.map (fn s => case StringCvt.scanString (scan StringCvt.DEC) s 
  172.                   of NONE => "OK" | _ => "WRONG")
  173.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  174.         "+1", "~1", "-1", "ff"];        
  175.  
  176. val test15a = 
  177.     List.map (chkScan StringCvt.BIN)
  178.              [("10010", 18),
  179.           (" \n\t10010crap", 18),
  180.           ("0w10010", 18),
  181.           (" \n\t0w10010crap", 18),
  182.           ("0", 0),
  183.           ("0w", 0),
  184.           ("0W1", 0),
  185.           ("0w ", 0),
  186.           ("0wx", 0),
  187.           ("0wX", 0),
  188.           ("0wx1", 0),
  189.           ("0wX1", 0),
  190. ˇÂˇˇ˝ˇ˚˛ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ˜üˇˇˇˇˇˇˇˇˇˇˇˇ¯ˇˇˇˇˇ‰ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ∑ˇˇˇˇˇˇüˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ˝ˇˇÌˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ–ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇø˜ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇµˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ¯flˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ